package cc.eddic.pratice.programmingparadigms.streampractice.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import cc.eddic.pratice.programmingparadigms.exam.impl.ExamServiceHrjImpl;
import cc.eddic.pratice.programmingparadigms.exam.impl.QuestionBankServiceHrjImpl;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAccuracy;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAndAnswer;
import cc.eddic.pratice.programmingparadigms.streampractice.StreamPractice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class StreamPracticeHrjImpl2 implements StreamPractice {
    @Override
    public List<QuestionAndAnswer> mapToQuestionAndAnswer(List<Question> questionList) {
        List<QuestionAndAnswer> qaList = new ArrayList<>();
        QuestionBankServiceHrjImpl questionBankService = new QuestionBankServiceHrjImpl();

        for (Question q : questionBankService.importCorrectAnswers("")) {
            Option o = q.getOptions().stream().filter(Option::isCorrectAnswer).findFirst().get();
            qaList.add(new QuestionAndAnswer(q.getNo(), q.getDescription(), o.getChoice(), o.getAnswer()));
        }

        return qaList;
    }

    public List<QuestionAccuracy> calculateAccuracy(List<Answer> answerList) {
        QuestionBankServiceHrjImpl questionBankService = new QuestionBankServiceHrjImpl();
        ExamServiceHrjImpl examServiceHrjImpl = new ExamServiceHrjImpl();
        Map<String, String> mapOfStudent = new HashMap<>();
        Map<Integer, Integer> correctNumOfQuestion = new HashMap<>();
        List<Answer> answers = examServiceHrjImpl.importAnswers("");

        List<AnswerModel> correctAnswer = questionBankService.readCorrectAnswers("");
        List<QuestionAccuracy> accuracyResult = new ArrayList<>();

        //考生人数
        for (Answer a : answers) {
            mapOfStudent.put(a.getStudent().getName(), "a");
        }

        //每道题答对人数
        for (AnswerModel am : correctAnswer) {
            int correctNum = 0;
            for (Answer a : answers) {
                if (am.getQuestionNo() == a.getQuestion().getNo()) {
                    if (am.getChoice() == a.getChoice()) {
                        correctNum = correctNum + 1;
                    }
                }
            }
            correctNumOfQuestion.put(am.getQuestionNo(), correctNum);
        }












        //计算每题正确率
        List<Question> questions = questionBankService.importCorrectAnswers("");
        for (Question q : questions) {
            double accuracy = (double) correctNumOfQuestion.get(q.getNo()) / mapOfStudent.keySet().size();
            System.out.println(accuracy);
            accuracyResult.add(new QuestionAccuracy(q.getNo(), q.getDescription(), accuracy));
        }

        return accuracyResult;
    }



}

